package gregorytersoff_controller;


import gregorytersoff_boundary.DecisionlinesGUI;
import gregorytersoff_model.Edge;
import gregorytersoff_model.Model;

import javax.swing.JButton;



public class EdgeController {
	private int xcoord;
	private int ycoord;
	private JButton button;
	private DecisionlinesGUI frame;
	private Model model;
	/**
	 * Calls the appropriate method when a location of a potential edge is selected by the user
	 * @param x The relative position of the left of the two lines connected by the potential edge
	 * @param y The height of the potential edge
	 * @param g The DecisionlinesGUI to access in order to update the LineCanvas and the error message label
	 * @param m The Model to access in order to create and/or check the validity of the potential edge
	 */
	public EdgeController (int x, int y, DecisionlinesGUI f, Model m)
	{
		xcoord = x;
		ycoord = y;
		frame = f;
		model = m;
	}
	/**
	 * Has the Model see if, and possibly add,  a potential edge with variables based on the x and y values given during creation of this class.
	 * If the edge is valid, it adds it to the LineCanvas so it can be drawn. It also updates the error message label  to reset it to the 
	 * default if the edge is valid, an error message explaining that the edge is invalid, or the final result if the final round just finished 
	 */
	public void process()
	{
		if(model.getStatus() && !model.getAllFinished())
		{
			Edge placeholder = model.setEdges(xcoord, ycoord);
			if(placeholder.getHeight() > -1)
			{
				frame.getLineCanvas().addEdge(placeholder);
				new ErrorController("Any errors will appear here", frame).process();
				if(model.getCurrentRound() > model.getRounds())
				{
					String results = new String(model.finish());
					new ErrorController(results, frame).process();
					frame.repaint();
				}
				else
				{
					new RoundController(frame).process();
					frame.repaint();
				}
			}
			else
			{
				new ErrorController("An edge has already been placed at that height, please select another height", frame).process();
			}
		}
		else if (!model.getAllFinished())
		{
			new ErrorController("Fill in an answer for each choice, then click the set choice button", frame).process();
		}
	}
}
